package com.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mall.common.utils.Constant;
import com.mall.product.dao.AttrDao;
import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.AttrGroupDao;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;

    @Autowired
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper= new QueryWrapper<AttrGroupEntity>();
        String key = (String) params.get(Constant.KEY);
        if(StringUtils.isNotBlank(key)){
            queryWrapper.like("attr_group_name",key);
        }
        if(catelogId !=0 ){
            queryWrapper.eq("catelog_id",catelogId);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> queryPage(Long groupId) {

       List<AttrAttrgroupRelationEntity> attrGroupList = attrgroupRelationService.lambdaQuery().eq(groupId != null,AttrAttrgroupRelationEntity::getAttrGroupId,groupId).list();

       List<Long> attrIdList = attrGroupList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());

       if(CollectionUtils.isEmpty(attrIdList)){
           return null;
       }

       List<AttrEntity> attrEntityList = attrService.listByIds(attrIdList);
        attrEntityList = attrEntityList.stream().filter(attrEntity -> attrEntity.getAttrType()==1 ).collect(Collectors.toList());
        return attrEntityList;

    }




    @Override
    public PageUtils queryNoRelationAttrListPage(Map<String, Object> params, Long groupId) {

      AttrGroupEntity attrGroup =  this.getById(groupId);
      QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_group_id",groupId);
        List<AttrAttrgroupRelationEntity>   attrAttrgroupRelationEntities = attrgroupRelationService.list(queryWrapper);
       List<Long> attrIdList = attrAttrgroupRelationEntities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        QueryWrapper<AttrEntity> attrQueryWrapper = new QueryWrapper<>();
        attrQueryWrapper.eq("catelog_id",attrGroup.getCatelogId());
        attrQueryWrapper.eq("attr_type",1);
        if(!CollectionUtils.isEmpty(attrIdList)){
          attrQueryWrapper.notIn("attr_id",attrIdList);
        }
        String key =(String) params.get(Constant.KEY);
        if(StringUtils.isNotBlank(key)){
            attrQueryWrapper.like("attr_name",key);
        }
        IPage<AttrEntity> page = attrService.page(
                new Query<AttrEntity>().getPage(params),attrQueryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupEntity> getAttrGroupByCatelogId(Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> queryWrapper= new LambdaQueryWrapper<AttrGroupEntity>();
        queryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId).orderByAsc(AttrGroupEntity::getSort);
        return this.list(queryWrapper);
    }


}